<?php

class BPlugin_url extends BPlugin
{

    /**
     * 格式化B2B搜索列表页URL
     *
     * @param mixed $query   原始URL串
     * @param bool $implode    是否为拼凑静态化URL行为
     */
    function urlFormat($query, $implode = false, $attach = '') {
        $flag_equal = '_';      //用于rewrite后的URL串中代替“=”符号
        $flag_separ = '-';      //用于rewrite后的URL串中代替“&”符号
        $exclude = array(   //该数组中的关键字将不被处理
            'local'
        );
        $result = array();
        if ($implode) { //静态化URL
            $url = '';
            $matched1 = preg_match_all('/(cd|od)\[(.*?)\]\=([^\&]*)/', $query, $matches1);
            $matched2 = preg_match_all('/(\w+)\=([^\&]*)/', $query, $matches2);
            if (!$matched1
                    && !$matched2) {
                return false;
            }
            foreach ($matches1[0] as $k=> $v) {
                if (!in_array($matches1[1][$k], $exclude)
                        && $matches1[3][$k]!='') {
                    $url .= $matches1[1][$k].'['.$matches1[2][$k].']'.$flag_equal.$matches1[3][$k].$flag_separ;
                }
            }
            unset($k, $v);
            foreach ($matches2[0] as $k=> $v) {
                if (!in_array($matches2[1][$k], $exclude)
                        && $matches2[2][$k]!='') {
                    $url .= $matches2[1][$k].$flag_equal.$matches2[2][$k].$flag_separ;
                }
            }
            unset($k, $v);
            return rtrim($url, $flag_separ);
        }
        //从静态URL恢复到动态数据
        foreach (explode($flag_separ, $query) as $v) {
            list($tmp,$tmp1) = explode($flag_equal, $v);
            if (preg_match('/(cd|od)\[(.*)\]/', $v, $match)) {
                $result[$match[1]][$match[2]] = $tmp1;
            }else {
                $result[$tmp] =$tmp1;
            }
        }
        return $result;
    }
}

?>
